/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networkresourcescanner;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.tree.DefaultMutableTreeNode;
import jcifs.smb.SmbFile;

/**
 *
 * @author HIRAYAMI
 */
public class MyTreeNode extends DefaultMutableTreeNode implements Runnable, MouseListener {

    public boolean isComputer;
    private String text;
    private SmbFile smbFile;
    public boolean isScanCompleted;
    private Thread t;
    private MyTree parentTree;

    public MyTreeNode(String text, MyTree parentTree) {
        super(text);
        this.parentTree = parentTree;
        init(text, null);
        try {
            setSmbFile(text);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        //Mytree te ase
        //    startScanning();
    }

    public MyTreeNode(String text, SmbFile smbFile) {
        super(text);
        init(text, smbFile);
        //W.expandAll(((MyTreeNode) this.getParent()).parentTree, new TreePath(this.getPath()), true);
    }

    private void init(String text, SmbFile smbFile) {
        this.text = text;
        this.smbFile = smbFile;
        this.isScanCompleted = true;
        this.isComputer = (smbFile == null);
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getText() {
        return this.text;
    }

    public void setSmbFile(SmbFile smbfile) {
        this.smbFile = smbfile;
    }

    public void setSmbFile(String ipString) throws MalformedURLException {
        this.smbFile = new SmbFile(new URL("smb://" + ipString + "/"));
    }

    public SmbFile getSmbFile() {
        return this.smbFile;
    }

    public boolean isScanning() {
        return t != null;
    }

    public void startScanning() {
        this.isScanCompleted = false;
        t = new Thread(this);
        t.start();
        //W.P(text + ":\t Scanning Started");
    }

    public void stopScanning() {
        if (t != null) {
            t.stop();
            t = null;
            //W.P(text + ":\t Scanning Stopped");
        }
    }

    boolean isAlive() {
        return true;
    }

    @Override
    public void run() {
        try {
            int i;
            if (!isAlive() && this.isComputer) {
                W.P(smbFile + " is Dead ");
                parentTree.removeNode(this);
                return;
            }

            SmbFile[] childs = smbFile.listFiles();
            if (childs.length > 0) {
                for (i = 0; i < childs.length; i++) {
                    String name = childs[i].getName();
                    if (!name.endsWith("$/")) {
                        this.add(new MyTreeNode(name.substring(0, name.length() - 1), childs[i]));
                    }
                }
            }
            //W.P(text + "Scan Completed>> found: " + childs.length);
        } catch (Exception ex) {
            //W.P("--------------------------------------------------------In MyTreeNode: TIMEOUT Exceptoin >> " + smbFile.getName());
            //ex.printStackTrace();
        }

        if (this.isComputer) {
            //W.P("finished on thread");
            parentTree.runNextThread();
        }
        this.isScanCompleted = true;
    }

    public void mouseClicked(MouseEvent me) {
        W.P("******" + smbFile.getPath());
        if (me.getClickCount() == 2) {
            //File f = new File("/mnt"+smbFile.get)
        }
    }

    public void mousePressed(MouseEvent me) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseReleased(MouseEvent me) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseEntered(MouseEvent me) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mouseExited(MouseEvent me) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
